package com.ciwong.epaper.widget.wheel.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.SchoolDetails;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description:实名登录学校搜索页面适配器
 * @author: wyk
 * @time: 2021/8/12 11:43
 */
public class SearchSchoolAdapter extends BaseAdapter {
    private Context context;

    private List<SchoolDetails> totalSchoolDetails;
    private LayoutInflater inflater;
    private String mKewWords;

    public SearchSchoolAdapter(Context context,
                               List<SchoolDetails> totalSchoolDetails,String kewWords) {
        this.context = context;
        this.totalSchoolDetails = totalSchoolDetails;
        inflater = LayoutInflater.from(context);

    }


    @Override
    public int getCount() {
        return totalSchoolDetails == null ? 0 : totalSchoolDetails.size();
    }

    @Override
    public Object getItem(int position) {
        return totalSchoolDetails.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SearchSchoolAdapter.ViewHolder holder;
        if (null == convertView) {
            holder = new SearchSchoolAdapter.ViewHolder();
            convertView = inflater.inflate(R.layout.activity_search_school_item, null);
            holder.cityNameTv = convertView.findViewById(R.id.city_name_tv);
            holder.area_text = convertView.findViewById(R.id.area_text);
            convertView.setTag(holder);
        } else {
            holder = (SearchSchoolAdapter.ViewHolder) convertView.getTag();
        }

        SchoolDetails cityEntity = totalSchoolDetails.get(position);
        SpannableString spannableString = matcherSearchTitle(context.getResources().getColor(R.color.black), cityEntity.getSchoolName(), mKewWords);
        holder.cityNameTv.setText(spannableString);
        holder.area_text.setText("(" + cityEntity.getAreaName() + ")");


        return convertView;
    }

    private class ViewHolder {
        private TextView cityNameTv;
        private TextView area_text;
    }

    /**
     * 刷新数据
     *
     * @param schoolDetails
     */
    public void setData(List<SchoolDetails> schoolDetails,String kewWords) {
        this.totalSchoolDetails = schoolDetails;
        this.mKewWords = kewWords;
        this.notifyDataSetChanged();
    }

    /**
     * 关键字高亮变色
     *
     * @param color 变化的色值
     * @param text 文字
     * @param keyword 文字中的关键字
     * @return 结果SpannableString
     */
    public static SpannableString matcherSearchTitle(int color, String text, String keyword) {
        SpannableString s = new SpannableString(text);
        keyword=escapeExprSpecialWord(keyword);
        text=escapeExprSpecialWord(text);
        if (text.contains(keyword)&&!TextUtils.isEmpty(keyword)){
            try {
                Pattern p = Pattern.compile(keyword);
                Matcher m = p.matcher(s);
                while (m.find()) {
                    int start = m.start();
                    int end = m.end();
                    s.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }catch (Exception e){
            }
        }
        return s;
    }


    /**
     * 转义正则特殊字符 （$()*+.[]?\^{},|）
     *
     * @param keyword
     * @return keyword
     */
    public static String escapeExprSpecialWord(String keyword) {
        try {
            if (!TextUtils.isEmpty(keyword)) {
                String[] fbsArr = {"\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|"};
                for (String key : fbsArr) {
                    if (keyword.contains(key)) {
                        keyword = keyword.replace(key, "\\" + key);
                    }
                }
            }
            return keyword;
        }catch (Exception e){
            return "";
        }

    }

}
